/**
 * 设置cookies  默认保存30天
 * @string name
 * @string value
 */
function setCookie(name, value) {
    var Days = 30; //默认保存30天
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toUTCString();
}

/**
 * 读取cookies
 * @string name
 * @returns 值或{null}
 */
function getCookie(name) {
    var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"), arr = document.cookie.match(reg);
    return arr && arr.length ? unescape(arr[2]) : null;
}


/**
 * 删除cookies
 * @string name
 */
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toUTCString();
}

/**
 * 设置cookies 保存时间
 * @string name
 * @string value
 * @string time 可选
 *      s是指秒，如30秒则是：s30
 *      m是指秒，如30秒则是：m30
 *      h是指小时，如12小时则是：h12
 *      d是指天数，如30天则是：d30
 */
function setCookieByTime(name, value, time) {
    var strsec = 30 * 24 * 60 * 60 * 1000; //默认保存30天
    if (time) {
        str = time;
        var str1 = str.substring(1, str.length) * 1;
        var str2 = str.substring(0, 1);
        switch (str2) {
            case "s":
                strsec = str1 * 1000;
                break;
            case "m":
                strsec = str1 * 60 * 1000;
                break;
            case "h":
                strsec = str1 * 60 * 60 * 1000;
                break;
            case "d":
                strsec = str1 * 24 * 60 * 60 * 1000;
                break;
        }
    }
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec * 1);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toUTCString();
}
